package com.uton.rfidcontroller.viewmodel.activity_model;

import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;

import com.uton.rfidcontroller.common.interfaces.INetController;
import com.uton.rfidcontroller.common.net.RetrofitFactory;
import com.uton.rfidcontroller.model.ScanCarDetailModel;
import com.uton.rfidcontroller.viewmodel.base.BaseViewModel;

import java.util.HashMap;

/**
 * Created by 王家宝 on 2017/10/16.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public abstract class WriteDetailActivityViewModel extends BaseViewModel {
    //RFID标签号
    public final ObservableField<String> cardId = new ObservableField<>("");
    //车辆品牌
    public final ObservableField<String> cBrand = new ObservableField<>("");
    //车牌号
    public final ObservableField<String> plateNumber = new ObservableField<>("");
    //车辆颜色
    public final ObservableField<String> carColour = new ObservableField<>("");
    //是否录入
    public final ObservableBoolean isWriteIn = new ObservableBoolean(false);

    public static final int STANDARD_LENGTH = 24;

    /**
     * 车辆Id
     */
    private String carId;
    /**
     * rfid的Id
     */
    private String rfidId;

    /**
     * RFID标签的Id
     */
    private String rfidLabelId;

    public WriteDetailActivityViewModel(String carId, String rfidId) {
        this.carId = carId;
        this.rfidId = rfidId;
        refreshData();
    }

    @Override
    public void refreshData() {
        HashMap<String, Object> tempData = new HashMap<>();
        tempData.put("carId", carId);
        tempData.put("carRfidId", rfidId);
        go(RetrofitFactory.getInstance().scanCarDetail(tempData), new INetController<ScanCarDetailModel>() {
            @Override
            public void onSuccess(ScanCarDetailModel response) {
                cardId.set(response.getRfidLabelId());
                cBrand.set(response.getCBrand());
                plateNumber.set(response.getPlateNumber());
                carColour.set(response.getCarColour());
                rfidLabelId = response.getRfidLabelId();
            }

            @Override
            public void onError() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    public String getRfidLabelId() {
        return rfidLabelId;
    }

    public void toCommit() {
        if (isWriteInSuccess() && isTidGetSuccess()) {
            showDialog();
            HashMap<String, Object> tempData = new HashMap<>();
            tempData.put("carRfidId", rfidId);
            tempData.put("tid", getTid());
            go(RetrofitFactory.getInstance().submitWriteIn(tempData), new INetController<String>() {
                @Override
                public void onSuccess(String response) {
                    dismissDialog();
                    doOnSuccess();
                }

                @Override
                public void onError() {
                    dismissDialog();

                }

                @Override
                public void onComplete() {
                    dismissDialog();
                    doOnComplete();
                }
            });
        } else {
            showCheckErrorMsg();
        }


    }

    public abstract void showCarDetail();

    public abstract void toWrite();

    public abstract void toRead();

    public abstract void doOnSuccess();

    public abstract void showDialog();

    public abstract void dismissDialog();

    public abstract void doOnComplete();

    public abstract boolean isWriteInSuccess();

    public abstract boolean isTidGetSuccess();

    public abstract void showCheckErrorMsg();

    public abstract String getTid();


}
